package com.cinc.ebc.pp.workOrderProcess.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cinc.ebc.pp.workOrder.entity.PpWorkOrder;
import com.cinc.ebc.pp.workOrder.service.IPpWorkOrderService;
import com.cinc.ebc.pp.workOrderProcess.entity.PpWorkOrderProcess;
import com.cinc.ebc.pp.workOrderProcess.mapper.PpWorkOrderProcessMapper;
import com.cinc.ebc.pp.workOrderProcess.model.PpWorkOrderProcessReportModel;
import com.cinc.ebc.pp.workOrderProcess.service.IPpWorkOrderProcessService;
import com.cinc.ebc.pp.workOrderProcess.vo.PpWorkOrderProcessPage;
import com.cinc.ebc.pp.workOrderProcess.vo.PpWorkOrderProcessReportPage;
import com.cinc.ebc.pp.workOrderReport.entity.PpWorkOrderReport;
import com.cinc.ebc.pp.workOrderReport.service.IPpWorkOrderReportService;
import com.cinc.mdm.modules.codeRule.service.IMdmCodeRuleService;
import org.jeecg.common.util.UUIDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 生产工单加工明细
 * @Author: jeecg-boot
 * @Date:   2024-04-05
 * @Version: V1.0
 */
@Service
public class PpWorkOrderProcessServiceImpl extends ServiceImpl<PpWorkOrderProcessMapper, PpWorkOrderProcess> implements IPpWorkOrderProcessService {

    @Autowired
    private IMdmCodeRuleService mdmCodeRuleService;

    @Autowired
    private IPpWorkOrderService ppWorkOrderService;

    @Autowired
    private IPpWorkOrderReportService ppWorkOrderReportService;

    @Override
    public IPage<PpWorkOrderProcessReportModel> reportPage(Page<PpWorkOrderProcessReportModel> page, PpWorkOrderProcessPage processPage) {
        return page.setRecords(baseMapper.selectReportList(page,processPage));
    }

    @Transactional
    @Override
    public void handleReport(PpWorkOrderProcessReportPage reportPage) {
        PpWorkOrderProcess workOrderProcess = baseMapper.selectById(reportPage.getId());
        if(!workOrderProcess.getStatus().equals("02")){
            throw new RuntimeException("工单明细当前状态不能汇报");
        }
        PpWorkOrder workOrder = ppWorkOrderService.getById(workOrderProcess.getWorkOrderId());
        if(!workOrder.getStatus().equals("11")){
            throw new RuntimeException("工单当前状态不能汇报");
        }
        BigDecimal reportQty = reportPage.getReportQty();
        BigDecimal checkQty = reportPage.getReportCheckQty();
        BigDecimal scrapQty = reportPage.getReportScrapQty();
        BigDecimal reworkQty = reportPage.getReportReworkQty();
        if(reportQty == null || reportQty.compareTo(BigDecimal.ZERO) <= 0){
            throw new RuntimeException("完工数量必须大于零");
        }
        if(reportQty.compareTo(checkQty.add(reworkQty).add(scrapQty)) != 0){
            throw new RuntimeException("汇报数量有误，完工数量 = 合格数量 + 废品数量 + 返修数量");
        }
        // 添加汇报单
        PpWorkOrderReport workOrderReportAdd = new PpWorkOrderReport();
        workOrderReportAdd.setId(UUIDGenerator.generate());
        workOrderReportAdd.setReportNo(mdmCodeRuleService.generatePpWorkOrderReportNo());
        workOrderReportAdd.setWorkOrderId(workOrderProcess.getWorkOrderId());
        workOrderReportAdd.setOrderNo(workOrder.getOrderNo());
        workOrderReportAdd.setWorkId(workOrderProcess.getWorkId());
        workOrderReportAdd.setWorkOrderPrcocessId(workOrderProcess.getId());
        workOrderReportAdd.setProcessNo(workOrderProcess.getProcessNo());
        workOrderReportAdd.setProcessName(workOrderProcess.getProcessName());
        workOrderReportAdd.setReportUser(reportPage.getReportUser());
        workOrderReportAdd.setReportQty(reportQty);
        workOrderReportAdd.setCheckQty(checkQty);
        workOrderReportAdd.setScrapQty(scrapQty);
        workOrderReportAdd.setReworkQty(reworkQty);
        workOrderReportAdd.setReportTime(reportPage.getCompleteTime());
        ppWorkOrderReportService.save(workOrderReportAdd);
        // 更新工单明细
        PpWorkOrderProcess workOrderProcessModify = new PpWorkOrderProcess();
        workOrderProcessModify.setId(workOrderProcess.getId());
        workOrderProcessModify.setQty(workOrderProcess.getQty().add(reportQty));
        workOrderProcessModify.setCheckQty(workOrderProcess.getCheckQty().add(checkQty));
        workOrderProcessModify.setScrapQty(workOrderProcess.getScrapQty().add(scrapQty));
        workOrderProcessModify.setReworkQty(workOrderProcess.getReworkQty().add(reworkQty));
        if(workOrderProcessModify.getCheckQty().compareTo(workOrder.getOrderQty()) >=0){
            workOrderProcessModify.setStatus("03");
            workOrderProcessModify.setActEndDate(new Date());
        }
        baseMapper.updateById(workOrderProcessModify);
        // 更新工单、开启下一工序
        LambdaQueryWrapper<PpWorkOrderProcess> workOrderProcessQuery = new LambdaQueryWrapper<>();
        workOrderProcessQuery.eq(PpWorkOrderProcess::getWorkOrderId, workOrderProcess.getWorkOrderId());
        List<PpWorkOrderProcess> workOrderProcessList = baseMapper.selectList(workOrderProcessQuery);
        workOrderProcessList.sort(Comparator.comparing(PpWorkOrderProcess::getSeqNo));
        long uncompleteCount = workOrderProcessList.stream().filter(data -> data.getSeqNo() == workOrderProcess.getSeqNo() && data.getStatus().equals("02")).count();
        if(uncompleteCount == 0){
            PpWorkOrderProcess workOrderProcessNext = workOrderProcessList.stream().filter(data -> data.getSeqNo() > workOrderProcess.getSeqNo()).findFirst().orElse(null);
            if(workOrderProcessNext == null){
                // 最后一序汇报完成，更新工单
                PpWorkOrder ppWorkOrderModify = new PpWorkOrder();
                ppWorkOrderModify.setId(workOrder.getId());
                if(workOrderProcessModify.getStatus().equals("03")){
                    ppWorkOrderModify.setStatus("12");
                }
                ppWorkOrderModify.setCompleteQty(workOrder.getCompleteQty().add(checkQty));
                ppWorkOrderService.updateById(ppWorkOrderModify);
            }else{
                // 下一序开启汇报
                List<PpWorkOrderProcess> workOrderProcessNextList = workOrderProcessList.stream()
                        .filter(data -> data.getSeqNo().equals(workOrderProcessNext.getSeqNo()))
                        .filter(data -> data.getStatus().equals("01")).collect(Collectors.toList());
                for(PpWorkOrderProcess processNext : workOrderProcessNextList){
                    PpWorkOrderProcess workOrderProcessStatusModify = new PpWorkOrderProcess();
                    workOrderProcessStatusModify.setId(processNext.getId());
                    workOrderProcessStatusModify.setStatus("02");
                    baseMapper.updateById(workOrderProcessStatusModify);
                }
            }
        }

    }
}
